{% extends 'admin_dashboard.html' %}

{% block title %}编辑客户{% endblock %}

{% block content %}
<div class="container">
  <h2>编辑客户信息</h2>
  <form id="editForm" method="POST">
    <div class="form-group">
      <label>客户编号</label>
      <input type="text" class="form-control" name="customerID" value="{{ customer.customerID }}" disabled>
    </div>
    <div class="form-group">
      <label>姓名</label>
      <input type="text" class="form-control" name="customerName" value="{{ customer.customerName }}" required>
    </div>
    <div class="form-group">
      <label>身份证号</label>
      <input type="text" class="form-control" name="PID" value="{{ customer.PID }}" required>
    </div>
    <div class="form-group">
      <label>联系电话</label>
      <input type="text" class="form-control" name="telephone" value="{{ customer.telephone }}" required>
    </div>
    <div class="form-group">
      <label>地址</label>
      <input type="text" class="form-control" name="address" value="{{ 'Null' if customer.address is none else customer.address }}">
    </div>
    <button type="submit" class="btn btn-primary">保存修改</button>
    <a href="{{ url_for('customers') }}" class="btn btn-secondary">取消</a>
  </form>
</div>

<script>
document.getElementById('editForm').addEventListener('submit', function (e) {
  e.preventDefault();
  const formData = new FormData(this);

  fetch(window.location.href, {
    method: 'POST',
    body: formData
  })
  .then(res => res.json())
  .then(data => {
    if (data.success) {
      alert("✅ 修改成功！");
      window.location.href = "{{ url_for('customers') }}";
    } else {
      alert("❌ " + data.message);
    }
  })
  .catch(err => {
    alert("⚠️ 请求失败，请稍后再试");
    console.error(err);
  });
});
</script>
{% endblock %}
